﻿using System;
using System.Xml;

namespace Abide.Classes
{
    public enum ReleaseCategory
    {
        Alpha,
        Beta,
        Stable
    }

    public class ReleaseInfo
    {
        public const string ReleaseURL = "https://abide.svn.codeplex.com/svn/releaseInfo.xml";

        public string Title
        {
            get { return title; }
        }
        public string[] Authors
        {
            get { return authors; }
        }
        public Release[] Releases
        {
            get { return releases; }
        }

        string title;
        string[] authors;
        Release[] releases;

        public static ReleaseInfo Load()
        {
            //Prepare Release Info
            ReleaseInfo info = new ReleaseInfo();

            try
            {
                //Prepare XML
                XmlDocument Document = new XmlDocument();
                Document.Load(ReleaseURL);
                XmlNode releaseInfo = Document["releaseInfo"];

                //Load Data
                info.title = releaseInfo["title"].InnerText;
                info.authors = new string[releaseInfo["authors"].ChildNodes.Count];
                for (int i = 0; i < info.authors.Length; i++)
                    info.authors[i] = releaseInfo["authors"].ChildNodes[i].InnerText;

                //Load Releases
                XmlNode Releases = releaseInfo["releases"];
                info.releases = new Release[Releases.ChildNodes.Count];
                for (int i = 0; i < info.releases.Length; i++)
                {
                    //Get Release
                    XmlNode Release = Releases.ChildNodes[i];

                    //Load Data
                    info.releases[i] = new Release();
                    info.releases[i].Url = Release["url"].InnerText;
                    info.releases[i].Version = new Version(Release["version"].InnerText);
                    info.releases[i].Category = (ReleaseCategory)Enum.Parse(typeof(ReleaseCategory), Release["category"].InnerText);
                }
            }
            catch { info = null; }

            //Return
            return info;
        }

        public class Release
        {
            public string Url { get; set; }
            public Version Version { get; set; }
            public ReleaseCategory Category { get; set; }
        }
    }
}
